package org.finmanager.data;

import org.finmanager.budget.Budget;
import org.finmanager.core.Category;

import java.io.File;
import java.io.PrintWriter;
import java.util.*;

import static org.finmanager.core.Constants.BUDGET_DATA_FILE_NAME;

/**
 * Created by Kelvin on 4/4/2014.
 *
 * This is an abstract list containing Budgets
 *
 * This class supports operations on the list
 */

//In our system, each budget item is stored as LIST in a .txt file

public class BudgetList {
	private ArrayList<Budget> list = new ArrayList<Budget>();
	private CategoryList categoryList;

	public BudgetList(CategoryList categoryList) {
		this.categoryList = categoryList;

		loadFile();
	}

	// Load budgets from the data file and store each budgets to a list
	// .txt file is used
	// Called in the start of the program
	private void loadFile() {
		try {
			File file = new File(BUDGET_DATA_FILE_NAME);

			// Create a new file if one does not exist
			// Add a zero to this new file (important)
			if (file.createNewFile()) {
				PrintWriter writer = new PrintWriter(file);
				writer.println(0);
				writer.close();
			}

			Scanner scanner = new Scanner(file);

			int numOfItems = Integer.parseInt(scanner.nextLine());
			for (int i = 0; i < numOfItems; ++i) {
				String categoryName = scanner.nextLine();

				double amount = Double.parseDouble(scanner.nextLine());
				double remaining = Double.parseDouble(scanner.nextLine());

				int year = Integer.parseInt(scanner.nextLine());
				int month = Integer.parseInt(scanner.nextLine());

				Category category = categoryList.findCategory(categoryName);

				Budget budget = new Budget(category, amount, remaining, year, month);
				list.add(budget);
			}
			scanner.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Save each budget in the list to the data file
	// Called every time the list is changed
	public void saveFile() {
		try {
			File file = new File(BUDGET_DATA_FILE_NAME);
			file.createNewFile();

			PrintWriter writer = new PrintWriter(file);

			writer.println(list.size());
			for (Budget budget : list) {
				writer.println(budget.getCategory().getName());
				writer.println(budget.getAmount());
				writer.println(budget.getRemaining());
				writer.println(budget.getDate().get(Calendar.YEAR));
				writer.println(budget.getDate().get(Calendar.MONTH));
			}
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//add budget
	public void addBudget(Budget budget) {
		list.add(budget);
		saveFile();
	}

	//delete budget
	public void deleteBudget(int index) {
		list.remove(index);
		saveFile();
	}

	public Budget getBudget(int index) {
		return list.get(index);
	}

	public void setBudget(int index, Budget budget) {
		list.set(index, budget);
		saveFile();
	}

	//get the total no. of existing budgets
	public int getBudgetCount() {
		return list.size();
	}

	public List<Budget> getList() {
		return Collections.unmodifiableList(list);
	}
}
